How to Upload
a File and How to Store it?
A quick and dirty Java Sockets API
program:
// imports java.net.Socket, java.io.*
void uploadFile(String hostAddr,
int port, String fileName) throws
FileNotFoundException, IOException
{
byte[] buf = new byte[1000];
int len = -1;
// connect to host at hostAddr,
port
Socket sock = new Socket(hostAddr,
port);
FileInputStream in = new FileInputStream(fileName);
OutputStream out = sock.getOutputStream();
while ( ( len = in.read(buf) ) !=
-1 ) {
out.write(buf, 0, len);
out.flush(); // manually flush
output stream for sockets
}
out.close();
sock.close();
in.close();
}
Paul-John A.
Related:
Java Books
Java Certification,
Programming, JavaBean and Object Oriented Reference Books
Return to : Java
Programming Hints and Tips
All the site contents are Copyright © www.erpgreat.com
and the content authors. All rights reserved.
All product names are trademarks of their respective
companies.
The site www.erpgreat.com is not affiliated with or endorsed
by any company listed at this site.
Every effort is made to ensure the content integrity.
Information used on this site is at your own risk.
The content on this site may not be reproduced
or redistributed without the express written permission of
www.erpgreat.com or the content authors.
|